package com.example.neteaseapplication;

import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import android.app.Activity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.neteaseapplication.entity.Song;
import com.example.neteaseapplication.inter.ILocalMusic;
import com.example.neteaseapplication.ui.cloud.CloudFragment;
import com.example.neteaseapplication.ui.discovery.DiscoveryFragment;
import com.example.neteaseapplication.ui.home.HomeFragment;
import com.example.neteaseapplication.ui.main.MainFragment;
import com.example.neteaseapplication.ui.video.VideoFragment;
import com.example.neteaseapplication.util.LocalMusicUtils;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends FragmentActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    private ImageButton mIbtnDrawer;
    private DrawerLayout mDrawerLayout;
    List<Fragment> mFragmentList = new ArrayList<>();
    private Button mBtnHome;
    private Button mBtnDiscovery;
    private Button mBtnCloud;
    private Button mBtnVideo;
    private ViewPager mViewPager;
    public static final int PAGE_ONE = 0;
    public static final int PAGE_TWO = 1;
    public static final int PAGE_THREE = 2;
    public static final int PAGE_FOUR = 3;
    private Song mSong;
    private ImageView mIvMusicImg;
    private TextView mTvMusicName;
    private TextView mTvSingerName;
    private ImageButton mIbPrev;
    private ImageButton mIbPlay;
    private ImageButton mIbNext;
    private List<Song> mSongList;
    private int mCurrentIndex = 0;
    private ImageButton mIbLocalMusic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//满屏显示
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        setAndroidNativeLightStatusBar(this, true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDrawerLayout = findViewById(R.id.drawer_layout);
        mIbtnDrawer = findViewById(R.id.ib_drawer);
        mIbtnDrawer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.openDrawer(GravityCompat.START);

            }
        });
        initView();
        mFragmentList.add(new HomeFragment());
        mFragmentList.add(new DiscoveryFragment());
        mFragmentList.add(new CloudFragment());
        mFragmentList.add(new VideoFragment());

        mBtnHome.setSelected(true);
        if (mSong == null) {
            mSongList = LocalMusicUtils.getmusic(this);
            mSong = mSongList.get(mCurrentIndex);
            initMusic();
        }

        MainFragment mainFragment = new MainFragment(getSupportFragmentManager(), mFragmentList);
        mViewPager.setCurrentItem(PAGE_ONE);
        mViewPager.addOnPageChangeListener(this);
        mViewPager.setAdapter(mainFragment);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = getIntent();
        mSong = mSongList.get(intent.getIntExtra("mainId", 0));
        initMusic();
        Log.e("resume","-----------"+mSong.toString());
    }

    /**
     * 加载音乐
     */
    public void initMusic() {
        Bitmap musicImg = LocalMusicUtils.getArtwork(this, mSong.getId(), mSong.getAlbumId(), false, false);
        mIvMusicImg.setImageBitmap(musicImg);
        mTvMusicName.setText(sortName(mSong.getName()));
        mTvSingerName.setText(mSong.getSinger());
    }

    /**
     * 切割音乐name的.mp3
     *
     * @param name
     * @return
     */
    public String sortName(String name) {
        return name.substring(0, name.length() - 4);
    }

    /**
     * 初始化控件
     */
    public void initView() {
        mBtnHome = this.findViewById(R.id.btn_home);
        mBtnDiscovery = findViewById(R.id.btn_discovery);
        mBtnCloud = findViewById(R.id.btn_cloud);
        mBtnVideo = findViewById(R.id.btn_video);
        mViewPager = findViewById(R.id.vp_content);
        mIvMusicImg = findViewById(R.id.iv_music_img);
        mTvMusicName = findViewById(R.id.tv_music_name_playing);
        mTvSingerName = findViewById(R.id.tv_music_singer_playing);
        mIbPrev = findViewById(R.id.ib_prev);
        mIbPlay = findViewById(R.id.ib_play_pause);
        mIbNext = findViewById(R.id.ib_next);

        mBtnHome.setOnClickListener(this);
        mBtnDiscovery.setOnClickListener(this);
        mBtnCloud.setOnClickListener(this);
        mBtnVideo.setOnClickListener(this);
        mIbPrev.setOnClickListener(this);
        mIbPlay.setOnClickListener(this);
        mIbNext.setOnClickListener(this);
    }

    public void toLocalMusic(){
        Intent intent = new Intent();
        intent.putParcelableArrayListExtra("songs", (ArrayList<? extends Parcelable>) mSongList);
        intent.putExtra("CurrentIndex", mCurrentIndex);
        startActivity(intent.setClass(MainActivity.this, LocalMusicActivity.class));
    }

    private static void setAndroidNativeLightStatusBar(Activity activity, boolean dark) {
        View decor = activity.getWindow().getDecorView();
        if (dark) {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    /**
     * 滑动点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        setSelected();
        int color = getResources().getColor(R.color.color_top);
        switch (v.getId()) {
            case R.id.btn_home:
                mViewPager.setCurrentItem(PAGE_ONE);
                mBtnHome.setSelected(true);
                mBtnHome.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
                mBtnHome.setTextColor(color);
                mBtnHome.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                break;
            case R.id.btn_discovery:
                mViewPager.setCurrentItem(PAGE_TWO);
                mBtnDiscovery.setSelected(true);
                mBtnDiscovery.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
                mBtnHome.setTextColor(color);
                mBtnDiscovery.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                break;
            case R.id.btn_cloud:
                mViewPager.setCurrentItem(PAGE_THREE);
                mBtnCloud.setSelected(true);
                mBtnCloud.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
                mBtnHome.setTextColor(color);
                mBtnCloud.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                break;
            case R.id.btn_video:
                mViewPager.setCurrentItem(PAGE_FOUR);
                mBtnVideo.setSelected(true);
                mBtnHome.setTextColor(color);
                mBtnVideo.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
                mBtnVideo.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                break;
            default:
                break;
        }
    }

    /**
     * 重置按钮选中
     */
    public void setSelected() {
        int color = getResources().getColor(R.color.color_main_text);
        mBtnHome.setSelected(false);
        mBtnDiscovery.setSelected(false);
        mBtnCloud.setSelected(false);
        mBtnVideo.setSelected(false);

        mBtnHome.setPressed(false);
        mBtnDiscovery.setPressed(false);
        mBtnCloud.setPressed(false);
        mBtnVideo.setPressed(false);

        mBtnHome.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        mBtnDiscovery.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        mBtnCloud.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        mBtnVideo.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));

        mBtnHome.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        mBtnDiscovery.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        mBtnCloud.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        mBtnVideo.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);

        mBtnHome.setTextColor(color);
        mBtnDiscovery.setTextColor(color);
        mBtnCloud.setTextColor(color);
        mBtnVideo.setTextColor(color);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        setSelected();
        int color = getResources().getColor(R.color.color_top);
        int CurrentIndex = mViewPager.getCurrentItem();
        switch (CurrentIndex) {
            case PAGE_ONE:
                mBtnHome.setPressed(true);
                mBtnHome.setTextColor(color);
                mBtnHome.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
                mBtnHome.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                break;
            case PAGE_TWO:
                mBtnDiscovery.setPressed(true);
                mBtnDiscovery.setTextColor(color);
                mBtnDiscovery.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
                mBtnDiscovery.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                break;
            case PAGE_THREE:
                mBtnCloud.setPressed(true);
                mBtnCloud.setTextColor(color);
                mBtnCloud.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
                mBtnCloud.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                break;
            case PAGE_FOUR:
                mBtnVideo.setPressed(true);
                mBtnVideo.setTextColor(color);
                mBtnVideo.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
                mBtnVideo.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageSelected(int position) {

    }

    /**
     * 页面滑动监听
     *
     * @param state
     */
    @Override
    public void onPageScrollStateChanged(int state) {
        setSelected();
        if (state == 2) {
            switch (mViewPager.getCurrentItem()) {
                case PAGE_ONE:
                    mBtnHome.setPressed(true);
                    break;
                case PAGE_TWO:
                    mBtnDiscovery.setPressed(true);
                    break;
                case PAGE_THREE:
                    mBtnCloud.setPressed(true);
                    break;
                case PAGE_FOUR:
                    mBtnVideo.setPressed(true);
                    break;
            }
        }
    }
}
